package com.ruoyi.park.service.impl;

import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.SecurityUtils;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.park.domain.ClCleanArea;
import com.ruoyi.park.domain.ClCleanRecord;
import com.ruoyi.park.domain.ClRecordArea;
import com.ruoyi.park.mapper.ClCleanAreaMapper;
import com.ruoyi.park.mapper.ClCleanRecordMapper;
import com.ruoyi.park.mapper.ClRecordAreaMapper;
import com.ruoyi.park.service.IClCleanRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 清洁记录Service业务层处理
 *
 * @author cjie
 * @date 2021-09-16
 */
@Service
public class ClCleanRecordServiceImpl implements IClCleanRecordService {
    @Autowired
    private ClCleanRecordMapper clCleanRecordMapper;
    @Autowired
    private ClRecordAreaMapper recordAreaMapper;
    @Autowired
    private ClCleanAreaMapper clCleanAreaMapper;

    /**
     * 查询清洁记录
     *
     * @param id 清洁记录ID
     * @return 清洁记录
     */
    @Override
    public ClCleanRecord selectClCleanRecordById(String id) {
        ClCleanRecord clCleanRecord = clCleanRecordMapper.selectClCleanRecordById(id);
        List<ClCleanArea> cleanAreaList = clCleanAreaMapper.selectClCleanAreaByRecordId(id);
        clCleanRecord.setCleanAreaList(cleanAreaList);
        return clCleanRecord;
    }

    /**
     * 查询清洁记录列表
     *
     * @param clCleanRecord 清洁记录
     * @return 清洁记录
     */
    @Override
    public List<ClCleanRecord> selectClCleanRecordList(ClCleanRecord clCleanRecord) {
        return clCleanRecordMapper.selectClCleanRecordList(clCleanRecord);
    }

    /**
     * 新增清洁记录
     *
     * @param clCleanRecord 清洁记录
     * @return 结果
     */
    @Override
    public AjaxResult insertClCleanRecord(ClCleanRecord clCleanRecord) {
        clCleanRecord.setId(IdUtil.getSnowflake(0, 0).nextIdStr());
        clCleanRecord.setCreateTime(DateUtils.getNowDate());
        clCleanRecord.setCreateBy(SecurityUtils.getUsername());

        if (clCleanRecord.getCleanAreaList() != null && clCleanRecord.getCleanAreaList().size() > 0) {
            clCleanRecord.getCleanAreaList().forEach(clCleanArea -> {
                ClRecordArea recordArea = new ClRecordArea();
                recordArea.setId(IdUtil.getSnowflake(0, 0).nextIdStr());
                recordArea.setRecordId(clCleanRecord.getId());
                recordArea.setAreaId(clCleanArea.getId());
                recordArea.setQualified(clCleanArea.getQualified());

                recordAreaMapper.insertClRecordArea(recordArea);
            });
        }

        if(clCleanRecordMapper.insertClCleanRecord(clCleanRecord)>0){
            return AjaxResult.success(clCleanRecord.getId());
        }else{
            return AjaxResult.error();
        }
    }

    /**
     * 修改清洁记录
     *
     * @param clCleanRecord 清洁记录
     * @return 结果
     */
    @Override
    public int updateClCleanRecord(ClCleanRecord clCleanRecord) {
        clCleanRecord.setUpdateTime(DateUtils.getNowDate());
        clCleanRecord.setUpdateBy(SecurityUtils.getUsername());
        return clCleanRecordMapper.updateClCleanRecord(clCleanRecord);
    }

    /**
     * 批量删除清洁记录
     *
     * @param ids 需要删除的清洁记录ID
     * @return 结果
     */
    @Override
    public int deleteClCleanRecordByIds(String[] ids) {
        Arrays.stream(ids).forEach(recordId -> recordAreaMapper.deleteClRecordAreaByRecordId(recordId));
        return clCleanRecordMapper.deleteClCleanRecordByIds(ids);
    }

    /**
     * 删除清洁记录信息
     *
     * @param id 清洁记录ID
     * @return 结果
     */
    @Override
    public int deleteClCleanRecordById(String id) {
        return clCleanRecordMapper.deleteClCleanRecordById(id);
    }
}
